


#ifndef PF_LINE_H
#define PF_LINE_H

#include "pf_atomicentity.h"
#include "lx_Export.h"

/**
 * Holds the data that defines a line.
 */
class LX_EXPORT PF_LineAttribute {
public:
    /**
     * Default constructor. Leaves the data object uninitialized.
     */
    PF_LineAttribute() {}

    PF_LineAttribute(const PF_Vector& startpoint,
                const PF_Vector& endpoint) {

        this->startpoint = startpoint;
        this->endpoint = endpoint;
    }

    friend class PF_Line;
    friend class WF_DrawLine;

    friend std::ostream& operator << (std::ostream& os, const PF_LineAttribute& ld) {
        os << "(" << ld.startpoint <<
        "/" << ld.endpoint <<
        ")";
        return os;
    }

public:
    PF_Vector startpoint;
    PF_Vector endpoint;
};


/**
 * Class for a line entity.
 *

 */
class LX_EXPORT PF_Line : public PF_AtomicEntity {
public:
    //PF_Line(PF_EntityContainer* parent);
    //PF_Line(const PF_Line& l);
    PF_Line(PF_Container* parent,
            const PF_LineAttribute& d);

    virtual PF_Entity* clone();
    /*{
    	cout << "cloning line\n";
    	return new PF_Line(*this);
    	//PF_Entity::copy(src);
           //if (src!=NULL && src->rtti()==LX_Define::EntityLine) {
           //    startpoint = src->getStartpoint();
           //    endpoint = src->getEndpoint();
           //}
       }*/

    virtual ~PF_Line();

    /**	@return LX_Define::EntityLine */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityLine;
    }
    /** @return true */
    virtual bool isEdge() const {
        return true;
    }

    /** @return Copy of data that defines the line. */
    PF_LineAttribute getData() const {
        return data;
    }
	
    virtual PF_VectorSolutions getRefPoints();

    /** @return Start point of the entity */
    virtual PF_Vector getStartpoint() const {
        return data.startpoint;
    }
    /** @return End point of the entity */
    virtual PF_Vector getEndpoint() const {
        return data.endpoint;
    }
    /** Sets the startpoint */
    void setStartpoint(PF_Vector s) {
        data.startpoint = s;
        calculateBorders();
    }
    /** Sets the endpoint */
    void setEndpoint(PF_Vector e) {
        data.endpoint = e;
        calculateBorders();
    }
	/** 
	 * @return Direction 1. The angle at which the line starts at 
	 * the startpoint. 
	 */
	double getDirection1() const {
		return getAngle1();
	}
	/** 
	 * @return Direction 2. The angle at which the line starts at 
	 * the endpoint.
	 */
	double getDirection2() const {
		return getAngle2();
	}
	virtual void moveStartpoint(const PF_Vector& pos);
	virtual void moveEndpoint(const PF_Vector& pos);
	virtual LX_Define::Ending getTrimPoint(const PF_Vector& coord, 
	          const PF_Vector& trimPoint);
	virtual void reverse();
    /** @return the center point of the line. */
    PF_Vector getMiddlepoint() {
        return (data.startpoint + data.endpoint)/2.0;
    }
    /** Sets the y coordinate of the startpoint */
    void setStartpointY(double val) {
        data.startpoint.y = val;
        calculateBorders();
    }
    /** Sets the y coordinate of the endpoint */
    void setEndpointY(double val) {
        data.endpoint.y = val;
        calculateBorders();
    }
	virtual bool hasEndpointsWithinWindow(PF_Vector v1, PF_Vector v2);

    /**
     * @return The length of the line.
     */
    virtual double getLength() {
        return data.startpoint.distanceTo(data.endpoint);
    }

    /**
     * @return The angle of the line (from start to endpoint).
     */
    virtual double getAngle1() const {
        return data.startpoint.angleTo(data.endpoint);
    }

    /**
     * @return The angle of the line (from end to startpoint).
     */
    virtual double getAngle2() const {
        return data.endpoint.angleTo(data.startpoint);
    }

    virtual PF_Vector getNearestEndpoint(const PF_Vector& coord,
                                         double* dist = NULL);
    virtual PF_Vector getNearestPointOnEntity(const PF_Vector& coord,
            bool onEntity=true, double* dist = NULL, PF_Entity** entity=NULL);
    virtual PF_Vector getNearestCenter(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestMiddle(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     const PF_Vector& coord,
                                     double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     bool startp);
    //virtual PF_Vector getNearestRef(const PF_Vector& coord,
    //                                 double* dist = NULL);
    virtual double getDistanceToPoint(const PF_Vector& coord,
                                      PF_Entity** entity=NULL,
                                      LX_Define::ResolveLevel level=LX_Define::ResolveNone,
									  double solidDist = LX_DOUBLEMAX);

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
    virtual void stretch(PF_Vector firstCorner,
                         PF_Vector secondCorner,
                         PF_Vector offset);
	virtual void moveRef(const PF_Vector& ref, const PF_Vector& offset);

    virtual void draw(PF_Painter* painter, PF_GraphicView* view, double patternOffset=0.0);

    friend std::ostream& operator << (std::ostream& os, const PF_Line& l);

    virtual void calculateBorders();

	
protected:
    PF_LineAttribute data;
    //PF_Vector startpoint;
    //PF_Vector endpoint;
}
;

#endif
